package cn.xiyue.production.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import cn.xiyue.production.R;
import cn.xiyue.production.base.CallableActivity;

public class LightActivity extends CallableActivity implements View.OnClickListener {

    private ImageView iv_light_status;
    private Button btn_open;
    private Button btn_close;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_light);
        initView();
    }

    private void initView() {
        iv_light_status = (ImageView) findViewById(R.id.iv_light_status);
        btn_open = (Button) findViewById(R.id.btn_open);
        btn_close = (Button) findViewById(R.id.btn_close);

        btn_open.setOnClickListener(this);
        btn_close.setOnClickListener(this);

        async(requestApi.getFactoryInfo(1), value -> {
            int isLighting = value.getData().get(0).getIsLighting();
            int resourceId = isLighting == 0 ? R.drawable.light_off : R.drawable.light_on;
            iv_light_status.setImageResource(resourceId);
        });
    }

    @Override
    public void onClick(View v) {
        int resourceId;
        int isLighting;
        String msg;
        switch (v.getId()) {
            case R.id.btn_open:
                resourceId = R.drawable.light_on;
                isLighting = 1;
                msg = "开灯成功";
                break;
            case R.id.btn_close:
                resourceId = R.drawable.light_off;
                isLighting = 0;
                msg = "关灯成功";
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + v.getId());
        }
        async(requestApi.setLighting(isLighting),value->{
            iv_light_status.setImageResource(resourceId);
            showToast(msg);
        });

    }
}